import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:exams_app/widget/hexcolor.dart';
import 'package:exams_app/widget/webview_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';
import 'package:get/get.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';
import 'myprofile_logic.dart';
import 'package:url_launcher/url_launcher.dart';



class MyProfilePage extends StatelessWidget {

  MyProfileLogic logic = Get.put(MyProfileLogic());

  int page = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我的收藏'),
        centerTitle: true,
      ),
      body: GetBuilder<MyProfileLogic>(
        builder: (logic) {

          return EasyRefresh.custom(
            firstRefresh: true,
            controller: logic.esController,
            emptyWidget: logic.emptyData(),
            header: MaterialHeader(),
            footer: MaterialFooter(),
            onRefresh: logic.onRefresh,
            onLoad: logic.onLoad,
            slivers: [
              SliverList(delegate: SliverChildBuilderDelegate(
                  _buildListItem, childCount: logic.dataList.length))
            ],
          );
        },
      ),
    );
  }

  Widget _buildListItem(BuildContext context, int index) {

    CollectionModelData model = logic.dataList[index];

    return SwipeActionCell(
      key: ValueKey(logic.dataList[index]),

      performsFirstActionWithFullSwipe: false,
      trailingActions: <SwipeAction>[
        SwipeAction(
           title: '取消收藏',
            widthSpace: 100,
            onTap: (CompletionHandler handler) async {
              handler(true);
             logic.requestDataWithCancelCollection('${await PersistentStorage().getStorage('uid')}',model.id);
            },
            color:HexColor('#F0504D')),
      ],
      child: GestureDetector(
        child: Container(
          color: Colors.transparent,
          margin: EdgeInsets.all(0),
          child: Row(
            // crossAxisAlignment:CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left: 15),
                child: SvgPicture.network(model.icon),
              ),
              Container(
                // color: Colors.red,
                width: Get.width-25 ,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: Get.width-100 ,
                      margin: EdgeInsets.only(top: 15, right: 0, left: 5),
                      child: Text(
                        '${model.name}',
                        style:
                        TextStyle(fontSize: 16, color: HexColor('#333333')),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 0, left: 5),
                      child: Text(
                        '上传于${model.pastTime}',
                        style:
                        TextStyle(fontSize: 12, color: HexColor('#999999')),
                      ),
                    ),
                    // Text('2020年最新出台政策.pdf',style: TextStyle(fontSize: 12,color: HexColor('#999999')),),
                    Container(
                      margin: EdgeInsets.only(top: 15),
                      color: HexColor('#EEEEEE'),
                      width: Get.width,
                      height: 1,
                      child: Text(''),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
        onTap: ()async {
          if(model.type == '文件夹'){
            // Get.to(DocumentJiaListPage(model.id,model.name,domain: '[]',));
          }else{
            // Get.to(MyWebView(url: model.previewUrl, title: '预览'));

            String url = '${model.download_url}';
            if(await canLaunch(url)){
              await launch(url);
            }else{
              throw 'url不能进行访问，异常。';
            }
          }
        },
      ),
    );
  }
}
